SOURCE_DIR=src
EBIN_DIR=ebin
DOC_DIR=doc
INCLUDE_DIR=include
INCLUDES=$(wildcard $(INCLUDE_DIR)/*.hrl)
SOURCES=$(wildcard $(SOURCE_DIR)/*.erl)
TARGETS=$(patsubst $(SOURCE_DIR)/%.erl, $(EBIN_DIR)/%.beam,$(SOURCES))
APP_IN=$(wildcard *.app.in)
APPS=$(patsubst %.app.in, $(EBIN_DIR)/%.app,$(APP_IN))

ERLC_OPTS=-I .. -I $(INCLUDE_DIR) -o $(EBIN_DIR) -Wall +debug_info # +native -v

ifeq ($(shell uname -s),Darwin)
SED=gnused
else
SED=sed
endif

all: dist

$(EBIN_DIR):
	-mkdir $(EBIN_DIR)

$(EBIN_DIR)/%.beam: $(SOURCE_DIR)/%.erl $(INCLUDES)
	erlc $(ERLC_OPTS) $<

$(EBIN_DIR)/%.app: %.app.in $(TARGETS)
	-rm -f $(EBIN_DIR)/*.app
ifeq (windows,$(findstring windows,$(shell uname -s)))
	ls -1 $(EBIN_DIR) | awk -F "." "{print $$1\",\"}" | sed -e "$$s/,//" >mods
	sed -e "/MODS/{r mods" -e ";d}" $< > $@
else
	ls -1 $(EBIN_DIR) | awk -F "." "{print \$$1\",\"}" | sed -e '$$s/,//' >mods
	sed -e '/MODS/{r mods' -e ';d}' $< > $@
endif
	rm -f mods

clean:
#	#rm -f $(EBIN_DIR)/*.beam 
#	#rm -f $(EBIN_DIR)/*.app
	-rm -r $(EBIN_DIR)

dist:$(EBIN_DIR) $(TARGETS) $(APPS)
	